package com.fish.web.account;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fish.account.po.Account;
import com.fish.account.po.Balance;
import com.fish.common.constant.Constant;
import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.service.account.AccountService;
import com.fish.utils.StringUtil;
import com.fish.vo.PageElementModel;

public class AccountAction extends BaseAction {
	private static final long serialVersionUID = -4020478609911502107L;
	private static final Log loger = LogFactory.getLog(AccountAction.class);
	private AccountService accountService;
	private List<PageElementModel> accountTypeList=Collections.emptyList();
	private Account account = new Account();
	private Balance userBalance = new Balance();
	private List<Account> accountList = new ArrayList<Account>();
	private String startDate;
	private String endDate;
	private QueryPage queryPage = new QueryPage();

	public String goToAccount() {
		return SUCCESS;
	}
	
	/**
	 * 查看余额
	 * @return
	 */
	public String lookBalance() {
		userBalance = accountService.queryBalanceByUser(getSessionUser().getUserId());
		return SUCCESS;
	}
	
	public String goToAccountList() {
		if (startDate != null && !"".equals(startDate) && endDate != null && !"".equals(endDate)) {
			account.setStartDate(convertToDate(startDate));
			account.setEndDate(convertToDate(endDate));
		}

		if(!getSessionUser().isAdmin()){
			account.setUserId(getSessionUser().getUserId());
		}
		
		Long count = accountService.queryAccountCountByObject(account);
		if (count > 0) {
			queryPage = new QueryPage(count, Constant.DEFAULT_PAGE_SIZE, queryPage.getCurPage());
		}
		accountList = accountService.queryAccountByObject(account, queryPage);
		initPageElement();
		return SUCCESS;
	}

	public String viewAccountInfo() {
		if (StringUtils.isNotBlank(account.getAccountId())) {
			//account = accountService.queryAccountByPrimaryKey(account);
		}
		return SUCCESS;
	}

	public String saveAccount() {
		try {
			if (!StringUtil.isEmptyString(account.getType())) {

				Double lastBalance = accountService.queryLastBalanceByBankId(account);
				// 流水帐号自动生成
				account.setAccountId(this.getNewDateUUID());
				calculateBalance(account, lastBalance);
				account.setOperateTime(new Date());
				accountService.insertAccount(account);
			}
		} catch (Exception e) {
			loger.debug("保存订单有错！");
			loger.debug(e);
		}
		account.setType("");// 清空后跳到accountList
		return goToAccountList();
	}
	
	private void calculateBalance(Account account, Double lastBalance) {
		if ("DRAWOUT".equals(account.getType()) || "REFUND".equals(account.getType()) || "PAYMENT".equals(account.getType())) {
			account.setBalance(lastBalance - account.getAmount());
		} else if ("RECHARGE".equals(account.getType())) {
			account.setBalance(lastBalance + account.getAmount());
		} else {
			account.setBalance(lastBalance);
		}
	}
	
	private void initPageElement(){
		accountTypeList=Constant.ACCOUNT_TYPE.getList();
	}
	
	private Date convertToDate(String dateStr){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			return format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @return the Account
	 */
	public Account getAccount() {
		return account;
	}

	/**
	 * @return the AccountList
	 */
	public List<Account> getAccountList() {
		return accountList;
	}

	/**
	 * @param AccountService
	 *            the AccountService to set
	 */
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	/**
	 * @param Account
	 *            the Account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<PageElementModel> getAccountTypeList() {
		return accountTypeList;
	}

	public void setAccountTypeList(List<PageElementModel> accountTypeList) {
		this.accountTypeList = accountTypeList;
	}

	public QueryPage getQueryPage() {
		return queryPage;
	}

	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public Balance getUserBalance() {
		return userBalance;
	}

	public void setUserBalance(Balance userBalance) {
		this.userBalance = userBalance;
	}
	
}